package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.UserDao;
import mil.af.amc.adpetracker.model.Organization;
import mil.af.amc.adpetracker.model.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.NonUniqueResultException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 *
 * @author Jason Ferguson
 */
@Repository("userDao")
public class HibernateUserDao extends HibernateGenericDao<User> implements UserDao {

    public HibernateUserDao(Class<User> type) {
        super(type);
    }

    public HibernateUserDao() {
        super(User.class);
    }

    private static Log log = LogFactory.getLog(HibernateUserDao.class);

//    public Collection loadAll() {
//
//        return getHibernateTemplate().find("from User");
//    }

    public User getByUsername(final String username) {

        Criteria userCriteria = sessionFactory.getCurrentSession().createCriteria(User.class);
        Criteria acctCriteria = userCriteria.createCriteria("account");


        //DetachedCriteria crit = DetachedCriteria.forClass(User.class);
        //DetachedCriteria acctCrit = crit.createCriteria("account");
        acctCriteria.add(Restrictions.eq("username", username.toLowerCase()));

        User result = null;
        try {
            result = (User) userCriteria.uniqueResult();
        } catch (NonUniqueResultException ex) {
            log.debug("getByUsername() throw NonUniqueResultException", ex);
            return null;
        }
        catch (HibernateException e) {
            log.debug("getByUsername() threw HibernateException", e);
        }

        return result;
        //return (User) getHibernateTemplate().findByCriteria(crit).get(0);
    }

    @SuppressWarnings({"unchecked"})
    public List<User> getAllByOrganization(Organization o) {

        Criteria userCriteria = sessionFactory.getCurrentSession().createCriteria(User.class);
        Criteria orgCriteria = userCriteria.createCriteria("assignedOrganization");
        orgCriteria.add(Restrictions.ilike("name", o.getName()));

        List<User> results = userCriteria.list();

        return results;
//        DetachedCriteria crit = DetachedCriteria.forClass(User.class);
//        DetachedCriteria organizationCrit = crit.createCriteria("assignedOrganization");
//        organizationCrit.add(Restrictions.ilike("name", o.getName()));
//
//        return getHibernateTemplate().findByCriteria(crit);

    }

    @SuppressWarnings({"unchecked"})
    public List<User> getUsersWithNoAssignedEquipment(Organization o) {

        Criteria userCriteria = sessionFactory.getCurrentSession().createCriteria(User.class);
        userCriteria.add(Restrictions.isEmpty("assignedEquipment"));
        Criteria orgCriteria = userCriteria.createCriteria("assignedOrganization");
        orgCriteria.add(Restrictions.eq("id", o.getId()));

        List<User> results = userCriteria.list();

        return results;
//        DetachedCriteria crit = DetachedCriteria.forClass(User.class);
//        crit.add(Restrictions.isEmpty("assignedEquipment"));
//        DetachedCriteria orgCrit = crit.createCriteria("assignedOrganization");
//        orgCrit.add(Restrictions.eq("id", o.getId()));
//
//        return (List<User>) getHibernateTemplate().findByCriteria(crit);
    }

    @SuppressWarnings({"unchecked"})
    public List<User> getUsersWithNoCpu(Organization o) {

//        DetachedCriteria crit = DetachedCriteria.forClass(User.class);
//        DetachedCriteria orgCrit = crit.createCriteria("assignedOrganization");
//        orgCrit.add(Restrictions.eq("id", o.getId()));

        Criteria userCriteria = sessionFactory.getCurrentSession().createCriteria(User.class);
        Criteria orgCriteria = userCriteria.createCriteria("assignedOrganization");
        orgCriteria.add(Restrictions.eq("id", o.getId()));
        sessionFactory.getCurrentSession().enableFilter("cpuOnly");

        List<User> results = userCriteria.list();

        return results;
//        
//        getHibernateTemplate().enableFilter("cpuOnly");
//
//        return (List<User>) getHibernateTemplate().findByCriteria(crit);
    }

}
